//http://192.168.2.123:8080/video
#include "v4a_hal.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include "bmp_encoder.h"
#include "USER_CFG.h"
#include "filecopy.h"
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include "fileupdate.h"
#include "spi.h"

#include <linux/watchdog.h>

#define IOCTL_LCD_BRIGHTNESS	_IOW('v', 27, unsigned int)	//brightness control
#define VIDEO_DISPLAY_ON	_IOW('v', 24, unsigned int)	//display on
#define VIDEO_DISPLAY_OFF	_IOW('v', 25, unsigned int)
#define FB_DEV "/dev/fb0"

int brightness=99;

extern int date_config_flag;
extern int time_config_en;
extern int date_config_en;
extern int real_offset;
extern int time_offset;
extern int date_offset;
extern int date_change;
extern int time_change;
extern int cur_bit;
extern int Dt_last_weekday;
extern int Direction_old;
extern int weather_flag;

extern int flr_map_flag;

char userbkgnd_bmp[4*1366*768] __attribute__ ((aligned(4))) ;
char userbkgnd_bmp_data[1366*768*4]  __attribute__ ((aligned(4))) ;

extern IMGNODE bkgnd_fbcopy;
extern IMGNODE bkgnd_fbdisplay;
extern IMGNODE overlay_fbcopy;
extern IMGNODE overlay_fbdisplay;
extern IMGNODE flex_fbdisplay;
extern unsigned int pic_addr;

extern unsigned int GUI_LCM_XMAX,GUI_LCM_YMAX,LCD_CLK;
extern REGION G_REG_DIR;
extern REGION G_REG_FLR;
extern REGION G_REG_TIME;
extern REGION G_REG_DAY;
extern REGION G_REG_WEEK;
extern REGION G_REG_ALARM;
extern REGION G_REG_BKGND;
extern REGION G_REG_LOGO;
extern REGION G_REG_OTHER;
extern REGION G_REG_VIDEO;
extern REGION G_REG_TXT;
unsigned int BKGND_wait;
unsigned int BKGND_effect;

extern unsigned int G_LFTTXT_txt_vfmode;
extern unsigned int G_LFTTXT_vf_speed;
extern unsigned int G_LFTTXT_txt_color;

REGION G_REG_DIR_T2;
REGION G_REG_FLR_T2;
REGION G_REG_ALARM_T2;

REGION REG_DES;
extern filestruct File_RAM[FOLDER_NUM][FILE_NUM_IN_FOLDER];

volatile unsigned int LIFT_FLAG;
volatile unsigned int Flag_CN;
int Native_Volume=0;
int Time_Bkgnd=0;
int Light_Off_Time=0;
int Sleep_Brightness=0;

_enum_DMODE G_DispMode = dmode_0C;

unsigned int display_tmp_addr=100;

extern IMGNODE ptr;
REGION DisplayRegion[11];
Flr_Map flr_map[255];

flr flr_n;
char dir_num;
char state_num;
char lift_info_bak[6];

LIFT_WORK_INFO lift_work_info;

char mp3_list[10][40];
int mp3_num=0;

int INSERT_FLAG = 0;
extern char flr_pic[40];

char * pDir_fixbkg[4] =
{
	"/mnt/nand1-2/LIFT/LIFT_0C/BKGND/FLR_BSD/fixedbkgnd.bmp",
	"/mnt/nand1-2/LIFT/LIFT_90C/BKGND/FLR_BSD/fixedbkgnd.bmp",
	"/mnt/nand1-2/LIFT/LIFT_180C/BKGND/FLR_BSD/fixedbkgnd.bmp",
	"/mnt/nand1-2/LIFT/LIFT_270C/BKGND/FLR_BSD/fixedbkgnd.bmp",
};

#define MAXLINE 80
#define SERV_PORT 8000

int usbdisk_is_insert()
{
	FILE *p=NULL;
	char bInterfaceClassNum[10];
	
	if( access("/sys/devices/platform/w55fa95-ohci/usb1/1-1/1-1:1.0/bInterfaceClass", 0) == 0)
	{
		p = fopen("/sys/devices/platform/w55fa95-ohci/usb1/1-1/1-1:1.0/bInterfaceClass","r");
		if(p==NULL)
		{
			printf("open bInterfaceClass failed\n");
			return 0;
		}

		memset(bInterfaceClassNum,0,10);
		fread(bInterfaceClassNum,10,1,p);
		fclose(p);
		p=NULL;
		if( (bInterfaceClassNum[0]=='0') && (bInterfaceClassNum[1]=='8') )
		{
			//printf("bInterfaceClass = %s\n",bInterfaceClassNum);
			return 1;
		}
	}
	
	
	if( access("/sys/devices/platform/w55fa95-ohci/usb1/1-2/1-2:1.0/bInterfaceClass", 0) == 0)
	{
		p = fopen("/sys/devices/platform/w55fa95-ohci/usb1/1-2/1-2:1.0/bInterfaceClass","r");
		if(p==NULL)
		{
			printf("open bInterfaceClass failed\n");
			return 0;
		}

		memset(bInterfaceClassNum,0,10);
		fread(bInterfaceClassNum,10,1,p);
		fclose(p);
		p = NULL;
		if( (bInterfaceClassNum[0]=='0') && (bInterfaceClassNum[1]=='8') )
		{
			//printf("bInterfaceClass = %s\n",bInterfaceClassNum);
			return 1;
		}
	}
	return 0;						

}

void *get_lift_pthread()
{
	char lift_urt_buffer[2];
	char buffer1[12];
	int nread=0,fd_serial,i,j,k;
	
	char dispbuf[4];
	char dispbuf_bak[4];
	char cmd[80];
	char cmd0[80];
	
	struct sockaddr_in servaddr;
	int sockfd, n;
	char buf[10];
	char str[INET_ADDRSTRLEN];
	socklen_t servaddr_len;
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
	servaddr.sin_port = htons(SERV_PORT);

	struct sockaddr_in servaddr1;
	int sockfd1;
	socklen_t servaddr_len1;
	sockfd1 = socket(AF_INET, SOCK_DGRAM, 0);
	bzero(&servaddr1, sizeof(servaddr1));
	servaddr1.sin_family = AF_INET;
	inet_pton(AF_INET, "127.0.0.1", &servaddr1.sin_addr);
	servaddr1.sin_port = htons(6666);

	  if((fd_serial=open_port(fd_serial,1))<0)
	  {
	       perror("open_port error");
	       return;
	  } 
	  if((i=set_opt(fd_serial,9600,8,'N',1))<0)
	  {
	       perror("set_opt error");
	       return;
	  } 
	while(1)
	{
		char *bf=buffer1;
		memset(lift_urt_buffer,0,2);
		memset(buffer1,0,12);

		nread=read(fd_serial,lift_urt_buffer,1);
		if(nread==0)
		{
			//dir_num=' ';
			//state_num=' ';
			continue;
		}
		memcpy(bf,lift_urt_buffer,1);
		if(lift_urt_buffer[0]==0x02)
		{
			for(i=0;i<11;i++)
			{
				bf++;
				nread=read(fd_serial,lift_urt_buffer,1);
				memcpy(bf,lift_urt_buffer,1);
			}
			if(buffer1[11]==0x03)
			{
				dir_num = buffer1[1];
				flr_n.flr1 = buffer1[2];
				flr_n.flr2 = buffer1[3];
				flr_n.flr3 = buffer1[4];
				state_num = buffer1[5];

				/*lift_work_info.run_dir = buffer1[1];
				lift_work_info.floor_num[0] = buffer1[2];
				lift_work_info.floor_num[1] = buffer1[3];
				lift_work_info.floor_num[2] = buffer1[4];
				lift_work_info.work_status = buffer1[5];
				lift_work_info.door_status = buffer1[6];
				*/
				
				n = sendto(sockfd1, buffer1, strlen(buffer1), 0, (struct sockaddr *)&servaddr1, sizeof(servaddr1));		
				//printf("buffer1[6] = %c\n",buffer1[6]);
				if(buffer1[6]=='2')
				{
					
				dispbuf[0] = flr_n.flr1;
				dispbuf[1] = flr_n.flr2;
				dispbuf[2] = flr_n.flr3;
				dispbuf[3] = '\0';
				
				if( (dispbuf[0]==dispbuf_bak[0]) && (dispbuf[1]==dispbuf_bak[1]) && (dispbuf[2]==dispbuf_bak[2]) )
					continue;
				memcpy(dispbuf_bak,dispbuf,3);
				if(flr_map_flag == 1)
				{
					for(i=0;i<255;i++)
					{
						if(!strcmp(dispbuf,flr_map[i].flr0))
						{	
							memcpy(dispbuf,flr_map[i].flr1,3);
							break;
						}
						continue;
					}
				}
				j = 0;
				for(k=0;k<4;k++)
				{
					if(dispbuf[k] == ' ')
						continue;
					else
					{
						dispbuf[j] = dispbuf[k];
						j++;
					}
				}	
				
				memset(cmd0,0,80);
				memset(cmd,0,80);
				memset(flr_pic,0,40);
				sprintf(flr_pic,"%s.bmp",dispbuf);
				
				sprintf(cmd0,"/mnt/nand1-2/LIFT/MP3/%s.mp3",dispbuf);
				if( access(cmd0,0)!=0 )
				{
					//sleep(2);
					//memset(flr_pic,0,40);
					//sprintf(flr_pic,"0.bmp");
					continue;
				}
				
				sprintf(cmd,"/mnt/appimg/madplay /mnt/nand1-2/LIFT/MP3/%s.mp3",dispbuf);
				buf[0] = 'w';
				buf[1] = '\0';
				n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
				usleep(100000);
				
				buf[0] = 'p';
				buf[1] = '\0';
				n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
				usleep(300000);
				system(cmd);
				buf[0] = 'w';
				buf[1] = '\0';
				n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
				usleep(200000);
				//memset(flr_pic,0,40);
				//sprintf(flr_pic,"0.bmp");
				}
						
			}
			else
			{
				//dir_num = ' ';
				//flr_n.flr1 = ' ';
				//flr_n.flr2 = ' ';
				//flr_n.flr3 = ' ';
				//state_num = ' ';
			}	
		}
		else
		{
			//dir_num = ' ';
			//flr_n.flr1 = ' ';
			//flr_n.flr2 = ' ';
			//flr_n.flr3 = ' ';
			//state_num = ' ';
		}
	}
	close(fd_serial);
	
}


int get_mp3_list()
{
	DIR *pdir = NULL;
	struct dirent *ptr = NULL;
	
	int i;
	
	for(i=0;i<10;i++)
	{
		memset(mp3_list[i],0,40);
	}
	
	i = 0;
	
	pdir = opendir("/mnt/nand1-2/LIFT/VIDEO/");
	if(pdir==NULL)
	{
		//printf("opendir /mnt/nand1-2/LIFT/TXT failed\n");
		return 1;
	}
	while((ptr=readdir(pdir))!=NULL)
	{
		if(strstr(ptr->d_name, ".mp3"))
		{			
			strcpy(mp3_list[mp3_num], ptr->d_name);			
			mp3_num++;
			if(mp3_num>=10)
				break;
		}
	}
	closedir(pdir);
	
}

int myplay(char* cmd)
{
    char volum[20];
    pid_t child, status;
    if ((child = fork()) < 0) 
    {
        perror("fork");
        return 1;
    }
    
    if(child == 0){
        execlp("./mplayer" , "mplayer" , "-slave" , "-quiet" , "-input" , "file=/fifo" , cmd, NULL);
        return -1;
    } 
    else {
		sleep(2);
		if(wait(&status) < 0) {
			perror("wait");
			exit(1);
		}
    }
    return status;
}

//音乐播放线程
void *get_lift_pthread2()
{
	char Music_File_Path[40];
	int i;
	int VIDE_FLAG = 0;
	sleep(15);
	while(1)
	{
		
		for(i=0;i<mp3_num;i++)
		{
			if(access("/mnt/nand1-2/mux.bin", 0) == 0)
				VIDE_FLAG = 1;
			if( (INSERT_FLAG == 0)||(VIDE_FLAG == 0) )
			{			
				memset(Music_File_Path, 0, 40);
				strcpy(Music_File_Path, "/mnt/nand1-2/LIFT/VIDEO/");
				strcat(Music_File_Path,mp3_list[i]);
				
				if(access(Music_File_Path, 0) == 0)
				{
						myplay(Music_File_Path);
				}
		    }			
			sleep(1);
		}
	}
	
	return 0;
}


void *get_lift_pthread1()
{
	
	struct sockaddr_in servaddr, cliaddr;
	socklen_t cliaddr_len;
	int sockfd, connfd_temp, tcp_socket_readlen,num;
	char buf[100];
	struct timeval outtime;
	memset(buf,0,100);
	outtime.tv_sec = 0;
	outtime.tv_usec = 1000;
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	setsockopt(sockfd, SOL_SOCKET,SO_RCVTIMEO, &outtime,sizeof(outtime));

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(8100);
	bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
	listen(sockfd, 2);

	int fd = open("/dev/watchdog", O_WRONLY);
	if (fd == -1) {
		perror("watchdog");
		exit(EXIT_FAILURE);
	}
	
	printf("\n\nOpen watchdog ok\n\n");

	while(1)
	{
	ioctl(fd, WDIOC_KEEPALIVE);
	for(num=0;num<8;num++)
	{
	tcp_socket_readlen=0;
	cliaddr_len = sizeof(cliaddr);
	tcp_socket_readlen=recvfrom(sockfd, buf, 80, 0, (struct sockaddr *)&cliaddr, &cliaddr_len);
	if(tcp_socket_readlen>0)
	{
		switch(buf[0])
		{
			case 'c':
				date_config_flag=1-date_config_flag;
				if(date_config_flag==1)
				{
					printf("config mod start\n");
				}
				else if(date_config_flag==0)
				{
					real_offset=0;
					date_offset=0;
					printf("config mod exit\n");
				}
        		break;

			case 'a':				
				date_change=1;					
				printf("add 1\n");
        		break;

			case 's':				
				date_change=-1;
				printf("sub 1\n");
        		break;
 
			case 'o':
				if(date_config_flag==1)
				{
					real_offset=real_offset+1;
					if(real_offset<=3)
						date_offset = real_offset;
					else if((real_offset>=4)&&(real_offset<=5))
						date_offset=real_offset+1;
					else if((real_offset>=6)&&(real_offset<=7))
						date_offset=real_offset+2;
					else if((real_offset>=8)&&(real_offset<=9))
						date_offset=real_offset+3;
					else if((real_offset>=10)&&(real_offset<=16))
						date_offset=real_offset+4;
					else if(real_offset>=17)
					{
						real_offset=0;
						date_offset=0;
					}
					printf("date_offset=%d\n",date_offset);
				}
        		break;
 
			default: 
        			printf("default\n");
        		break;
		}

	}
			
		usleep(100000);
	}
		cur_bit=1-cur_bit;
	}
	close(fd);
}


int Date_Time_Config(void)
{
	
	unsigned int temp;	
	char buf[100];
	char station_data[100];
	int readlen,index;
	FILE *p;
	int i;
	int dpx=0;
	int dpy=324;
	int width_sum=0;
	int width_max=0;
	int height_max=0;
	IMGNODE userbkgnd_pic;
	BMPINFO* info;
	
	int spaceoffset;
	
	time_t timer =time(NULL);
	struct tm *sys_time = localtime(&timer);
	int character_code;
	memset(station_data,0,100);

	//年
	station_data[0] = '2';
	station_data[1] = '0';
	temp = sys_time->tm_year % 100;
	station_data[2] = (temp / 10)+'0';
	temp = sys_time->tm_year % 10;
	station_data[3] = temp+'0';

	station_data[4] = '.';
	
	//月
	temp = (sys_time->tm_mon+1) / 10;
	station_data[5] = temp+'0';
	temp = (sys_time->tm_mon+1) % 10;
	station_data[6] = temp+'0';

	station_data[7] = '.';

	//日
	temp = sys_time->tm_mday / 10;
	station_data[8] = temp+'0';
	temp = sys_time->tm_mday % 10;
	station_data[9] = temp+'0';
	
	station_data[10] = '-';

	//时
	temp = sys_time->tm_hour / 10;
	station_data[11] = '0' + temp;
	temp = sys_time->tm_hour % 10;
	station_data[12] = '0' + temp;
	
	station_data[13] = ':';
	
	//分
	temp = sys_time->tm_min / 10;
	station_data[14] = '0' + temp;
	temp = sys_time->tm_min % 10;
	station_data[15] = '0' + temp;
	
	//station_data[16] = ':';
	
	station_data[16] = '\0';
	
	//秒
	//temp = sys_time->tm_sec / 10;
	//station_data[17] = '0' + temp;
	//temp = sys_time->tm_sec % 10;
	//station_data[18] = '0' + temp;
	
	//station_data[19] = '\0';
	
	if(date_config_flag==1)
	if(date_offset<16)
	{
		
		station_data[date_offset]=station_data[date_offset]+date_change;
		if( station_data[date_offset] > '9')
			station_data[date_offset]='0';
		else if( station_data[date_offset] < '0')
			station_data[date_offset]='9';
		
		if((station_data[2]-'0')*10+station_data[3]-'0'>=36)
		{
			station_data[2]='3';
			station_data[3]='0';
		}

		if((station_data[5]-'0')*10+station_data[6]-'0'>=13)
		{
			station_data[5]='1';
			station_data[6]='0';
		}

		if((station_data[8]-'0')*10+station_data[9]-'0'>=32)
		{
			station_data[8]='3';
			station_data[9]='0';
		}

		if((station_data[5]-'0')*10+station_data[6]-'0'==0)
		{
			station_data[5]='0';
			station_data[6]='1';
		}

		if((station_data[8]-'0')*10+station_data[9]-'0'==0)
		{
			station_data[8]='0';
			station_data[9]='1';
		}
		
		if((station_data[11]-'0')*10+station_data[12]-'0'>=24)
		{
			station_data[11]='2';
			station_data[12]='0';
		}

		if((station_data[14]-'0')*10+station_data[15]-'0'>=60)
		{
			station_data[14]='5';
			station_data[15]='0';
		}

		if(date_change!=0)
		{
		printf("date_offset=%d\n",date_offset);
		memset(buf,0,100);
		sprintf(buf,"date 20%c%c.%c%c.%c%c-%c%c:%c%c",station_data[2],station_data[3],station_data[5],station_data[6],station_data[8],station_data[9],station_data[11],station_data[12],station_data[14],station_data[15]);
		system(buf);
		system("hwclock -w");
		date_change=0;
		}
		
		if(cur_bit)
			station_data[date_offset]=' ';
	}
	Display_Date_Time(station_data);
}

int Volume_Config(void)
{
	FILE * VOLUME;
	char buffer[20];
	if( (date_offset==16) && (date_change!=0) )
	{
		Native_Volume += date_change*5;		
		if(Native_Volume<0)
			Native_Volume=0;
		if(Native_Volume>99)
			Native_Volume=99;
			
		VOLUME = fopen("/mnt/nand1-1/volume.cfg","wb");
		fputc(Native_Volume,VOLUME);
		fclose(VOLUME);
		system("sync");
		date_change=0;
	}
	memset(buffer,0,20);
	sprintf(buffer,"VOL=[%d]",Native_Volume);
	
	if( (cur_bit==1) && (date_offset==16) )
	buffer[strlen(buffer)-2]=' ';
	
	Display_Date_Time(buffer);
}

int Language_Config(void)
{
	FILE * VOLUME;
	if( (date_offset==17) && (date_change!=0) )
	{
		Flag_CN = 1-Flag_CN;					
		VOLUME = fopen("/mnt/nand1-1/language.txt","wb");
		fputc(Flag_CN,VOLUME);
		fclose(VOLUME);
		system("sync");
		date_change=0;
	}
	
	if( (cur_bit==1) && (date_offset==17) )
		Display_Date_Time("LANGUAGE=  ");
	else
	{
		if(Flag_CN)
			Display_Date_Time("LANGUAGE=CN");
		else
			Display_Date_Time("LANGUAGE=EN");
		
	}
	
}

int Bkgnd_Time_Config(void)
{
	FILE * VOLUME;
	char buffer[20];
	if( (date_offset==18) && (date_change!=0) )
	{
		Time_Bkgnd += date_change;		
		if(Time_Bkgnd<0)
			Time_Bkgnd=0;
		if(Time_Bkgnd>99)
			Time_Bkgnd=99;					
		VOLUME = fopen("/mnt/nand1-1/bkgndtime.txt","wb");
		fputc(Time_Bkgnd,VOLUME);
		fclose(VOLUME);
		system("sync");
		date_change = 0;
	}
	
	memset(buffer,0,20);
	sprintf(buffer,"BKGND_TIME=[%2d]",Time_Bkgnd);
	if( (cur_bit==1) && (date_offset==18) )
	buffer[strlen(buffer)-2]=' ';
	
	Display_Date_Time(buffer);
	
}

int Light_Off_Time_Config(void)
{
	FILE * VOLUME;
	char buffer[20];
	if( (date_offset==19) && (date_change!=0) )
	{
		Light_Off_Time+= date_change;		
		if(Light_Off_Time<1)
			Light_Off_Time=1;
		if(Light_Off_Time>99)
			Light_Off_Time=99;					
		VOLUME = fopen("/mnt/nand1-1/light.txt","wb");
		fputc(Light_Off_Time,VOLUME);
		fclose(VOLUME);
		system("sync");
		date_change = 0;
	}
	
	memset(buffer,0,20);
	sprintf(buffer,"SLEEP_WAIT_TIME=[%2d]",Light_Off_Time);
	if( (cur_bit==1) && (date_offset==19) )
		buffer[strlen(buffer)-2]=' ';
	
	Display_Date_Time(buffer);
	
}

int Sleep_Brightness_Config(void)
{
	FILE * VOLUME;
	char buffer[20];
	if( (date_offset==20) && (date_change!=0) )
	{
		Sleep_Brightness += date_change*5;		
		if(Sleep_Brightness<0)
			Sleep_Brightness=0;
		if(Sleep_Brightness>99)
			Sleep_Brightness=99;					
		VOLUME = fopen("/mnt/nand1-1/bl.txt","wb");
		fputc(Sleep_Brightness,VOLUME);
		fclose(VOLUME);
		system("sync");
		date_change = 0;
	}
	
	memset(buffer,0,20);
	sprintf(buffer,"SLEEP_BRIGHTNESS=[%2d]",Sleep_Brightness);
	if( (cur_bit==1) && (date_offset==20) )
		buffer[strlen(buffer)-2]=' ';
	
	Display_Date_Time(buffer);
	
}

int Config_Init(void)
{
	FILE *VOLUME;
	VOLUME = fopen("/mnt/nand1-1/volume.cfg","rb");		
	if(VOLUME == NULL)
	{
		VOLUME = fopen("/mnt/nand1-1/volume.cfg","wb");
		fputc(20,VOLUME);
		fclose(VOLUME);
		system("sync");
		
		Native_Volume=20;
	}
	else
	{
		Native_Volume=fgetc(VOLUME);	
		fclose(VOLUME);
	}
	
	VOLUME = fopen("/mnt/nand1-1/language.txt","rb");		
	if(VOLUME == NULL)
	{
		VOLUME = fopen("/mnt/nand1-1/language.txt","wb");
		fputc(1,VOLUME);
		fclose(VOLUME);
		system("sync");
		
		Flag_CN=1;
	}
	else
	{
		Flag_CN=fgetc(VOLUME);	
		fclose(VOLUME);
	}
	
	VOLUME = fopen("/mnt/nand1-1/bkgndtime.txt","rb");		
	if(VOLUME == NULL)
	{
		VOLUME = fopen("/mnt/nand1-1/bkgndtime.txt","wb");
		fputc(5,VOLUME);
		fclose(VOLUME);
		system("sync");
		
		Time_Bkgnd=5;
	}
	else
	{
		Time_Bkgnd=fgetc(VOLUME);	
		fclose(VOLUME);
	}
	
	VOLUME = fopen("/mnt/nand1-1/light.txt","rb");		
	if(VOLUME == NULL)
	{
		VOLUME = fopen("/mnt/nand1-1/light.txt","wb");
		fputc(15,VOLUME);
		fclose(VOLUME);
		system("sync");
		
		Light_Off_Time=15;
	}
	else
	{
		Light_Off_Time=fgetc(VOLUME);	
		fclose(VOLUME);
	}
	
	VOLUME = fopen("/mnt/nand1-1/bl.txt","rb");		
	if(VOLUME == NULL)
	{
		VOLUME = fopen("/mnt/nand1-1/bl.txt","wb");
		fputc(30,VOLUME);
		fclose(VOLUME);
		system("sync");
		
		Sleep_Brightness=30;
	}
	else
	{
		Sleep_Brightness=fgetc(VOLUME);	
		fclose(VOLUME);
	}
	
}

int enable_backlight(int bn)
{
	int fb_fd;
	brightness = bn;
	if ((fb_fd = open(FB_DEV, O_RDWR)) < 0) {
		printf("open device %s failed", FB_DEV);
		return -1;
	}
	ioctl(fb_fd, VIDEO_DISPLAY_ON);
	ioctl(fb_fd, IOCTL_LCD_BRIGHTNESS, &brightness);
	//sleep(1);
	close(fb_fd);
	return 0;
}

int Display_Fixedbkgnd()
{
	int i=0;
	IMGNODE userbkgnd_pic;
	
	FILE *p = fopen(pDir_fixbkg[G_DispMode],"rb");
	if(p!=NULL)
	{
	
		int readlen = 0;
		readlen = fread(userbkgnd_bmp,4*1366*768,1,p);

		BMPINFO* info = bmp_getinfo(userbkgnd_bmp);
		if(info->bpp==16)
		{
		for(i = 0;i<(int)(info->height);i++)
			memcpy(&userbkgnd_bmp_data[((int)(info->height)-1-i)*2*(int)(info->width)],info->pixel_array+i*(int)(info->width)*2,(int)(info->width)*2);
		}
		else
		{
			for(i = 0;i<(int)(info->height);i++)
			memcpy(&userbkgnd_bmp_data[((int)(info->height)-1-i)*4*(int)(info->width)],info->pixel_array+i*(int)(info->width)*4,(int)(info->width)*4);
			unsigned short * pu16Temp;
			unsigned char u8R, u8G, u8B;	
			
			pu16Temp = (unsigned short *)userbkgnd_bmp;
			memset(userbkgnd_bmp,0,4*1366*768);
			for ( i = 0; i < (int)(info->width)*(int)(info->height); i++ )
			{
				u8B = userbkgnd_bmp_data[i*4];
				u8G = userbkgnd_bmp_data[i*4+1];
				u8R = userbkgnd_bmp_data[i*4+2];
				
				pu16Temp[i] = ( ( u8R&0xff )>>3 )<<11 | ( (u8G&0xff)>>2 )<<5 | (u8B&0xff )>>3;
			
			}		
			memcpy(userbkgnd_bmp_data,userbkgnd_bmp,4*1366*768);	
		}
		
		userbkgnd_pic.img_base = bkgnd_fbcopy.img_base;
		userbkgnd_pic.img_xlen = info->width;
		userbkgnd_pic.img_ylen = info->height;
		userbkgnd_pic.img_bpp = 16;
	}
	else
	{
		short *puser;
		printf("open fixedbkgnd.bmp failed\n");
		memset(userbkgnd_bmp_data,0,1366*768*4);
		puser = (short *)userbkgnd_bmp_data;
		for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX/2;i++)
		{
			*puser = 0x001F;//BLUE
			puser++;
		}
		for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX/2;i++)
		{
			*puser = 0xF800;//RED
			puser++;
		}
		
		userbkgnd_pic.img_base = bkgnd_fbcopy.img_base;
		userbkgnd_pic.img_xlen = GUI_LCM_XMAX;
		userbkgnd_pic.img_ylen = GUI_LCM_YMAX;
		userbkgnd_pic.img_bpp = 16;
		
	}
	v4a_drawrect(&bkgnd_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
	v4a_fillvram(userbkgnd_pic.img_base,userbkgnd_pic.img_xlen*userbkgnd_pic.img_ylen/2,(int *)userbkgnd_bmp_data);
	
	int sx0 = 0;
	int sy0 = 0;
	
	int sx = 0;
	int sy = 0;	
	
	if(G_DispMode == dmode_0C)
	{			
		sx = sx0;
		sy = sy0;
	}
	else if(G_DispMode == dmode_90C)
	{		
		sx = GUI_LCM_XMAX-sy0-userbkgnd_pic.img_xlen;
		sy = sx0;
	}
	else if(G_DispMode == dmode_180C)
	{			
		sx = GUI_LCM_XMAX-sx0-userbkgnd_pic.img_xlen;
		sy = GUI_LCM_YMAX-sy0-userbkgnd_pic.img_ylen;		
	}
	else if(G_DispMode == dmode_270C)
	{				
		sx = sy0;
		sy = GUI_LCM_YMAX-sx0-userbkgnd_pic.img_ylen;
	}
	
	v4a_copyimage(&userbkgnd_pic,&bkgnd_fbdisplay,gui_point(sx,sy));
	
	return 0;
}

int main()
{	
	int i;
	int num = 0;
	int cc;	
	int bn_flag=0;
	char client_id[40];
	char print_mac[40];
	memset(flr_pic,0,40);
	sprintf(flr_pic,"0.bmp");
	
	memset(client_id,0,40);
	FILE *ipid_fd=fopen("/v4asys/mac","r");
	if(ipid_fd!=NULL)
	{	     
		fgets(client_id,40,ipid_fd);
		fclose(ipid_fd);
	}
	memset(print_mac,0,40);
	strcpy(print_mac,"Terminal MAC: ");
	strcat(print_mac,client_id);

	pthread_t tid_get_lift, tid_get_lift1, tid_get_lift2;
	
	pthread_create(&tid_get_lift,NULL,get_lift_pthread,NULL); 
	pthread_create(&tid_get_lift1,NULL,get_lift_pthread1,NULL); 
	pthread_create(&tid_get_lift2,NULL,get_lift_pthread2,NULL); 
	srand((unsigned)time(NULL));
	
	memset(lift_info_bak,0,6);
	lift_info_bak[5] = '\0';

	G_DispMode = GetSysCfg();
	G_REG_VIDEO.x_len = G_REG_VIDEO.x_len/2*2;
	printf("\n\n%d\n\n",G_DispMode);
	if((G_DispMode == dmode_90C) || (G_DispMode == dmode_270C))
	{
		i = GUI_LCM_XMAX;
		GUI_LCM_XMAX = GUI_LCM_YMAX;
		GUI_LCM_YMAX = i;
	}
	
	if(G_DispMode == dmode_error)
	{
		G_DispMode = dmode_0C;
		LCD_CLK = 1;
		GUI_LCM_XMAX = 800;
		GUI_LCM_YMAX = 600;
	}
	
	InitialGUI();
	InitialFileRAMSruct();
	v4a_initial();
	v4a_initscreen(GUI_LCM_XMAX,GUI_LCM_YMAX);
	
	DisplayRegion[9].x_len = DisplayRegion[9].x_len/2*2;
	DisplayRegion[9].y_len = DisplayRegion[9].y_len/2*2;
	if( (DisplayRegion[9].x_len<=1024)&&(DisplayRegion[9].y_len<=720) )
		v4a_setvideoarea(gui_rect(DisplayRegion[9].s_x,DisplayRegion[9].s_y,DisplayRegion[9].x_len,DisplayRegion[9].y_len));
	else
		InitLCM(DisplayRegion[9].x_len,DisplayRegion[9].y_len);
		
	v4a_drawrect(&overlay_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
	v4a_drawrect(&bkgnd_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
		
	Display_Print_Init();
	Display_Print("Software Version: NJ_V4A01_1.2.3");
	enable_backlight(99);
	v4a_enablescreen(1);
	v4a_displaybkgnd(1);
	sleep(2);	
	Display_Fixedbkgnd();
	
	CopyAllToRAM();
	floormap_init();
	get_mp3_list();
	Config_Init();
	v4a_drawrect(&overlay_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
START:		
		
	v4a_displaybkgnd(1);
	v4a_displayvideo(1);
	v4a_displayoverlay(1);

	DisplayLogo();	
	
	while(1){
		int y,z;
		printf("cur_brightness = %d,Sleep_Brightness = %d\n",brightness,Sleep_Brightness);
		//display_char_func();
		DisplayWeather();
		for(y=0;y<10;y++)
		{
			
			if( (lift_info_bak[0] == dir_num)&&(lift_info_bak[1] == state_num)&&(lift_info_bak[2] == flr_n.flr1)&&(lift_info_bak[3] == flr_n.flr2)&&(lift_info_bak[4] == flr_n.flr3) )
				bn_flag++;
			else
			{
				lift_info_bak[0] = dir_num;
				lift_info_bak[1] = state_num;
				lift_info_bak[2] = flr_n.flr1;
				lift_info_bak[3] = flr_n.flr2;
				lift_info_bak[4] = flr_n.flr3;
				
				if( brightness!=99 )
					enable_backlight(99);
					
				bn_flag = 0;
			}
			if(bn_flag>=30*Light_Off_Time)
			{				
				if( brightness!=Sleep_Brightness )
				enable_backlight(Sleep_Brightness);
				bn_flag = 0;
			}
			
			display_user_picture();
			display_picture_in_video();
			for(z=0;z<5;z++)
			{					
				if(date_config_flag)
					goto DATECFG;
				DisplayTime();
			
				DisplayDay();
				
				DisplayWeek();
				usleep(200000);
		
				DisplayDir(dir_num);
				DisplayFloor(flr_n);
				//display_flr_picture();
				DisplayState(state_num);
				
				if( cc=usbdisk_is_insert() )
				{
					if(INSERT_FLAG == 0)
					{
					sleep(8);
					system("mount -t ext3 /dev/sdb1 /mnt/sdcard");
					system("mount -t vfat /dev/sdb1 /mnt/sdcard");
					system("mount -t ext3 /dev/sdc1 /mnt/sdcard");
					system("mount -t vfat /dev/sdc1 /mnt/sdcard");
					
					if( access("/mnt/sdcard/mux.bin", 0) == 0 )
					{
						system("touch /mnt/nand1-2/mux.bin");
						system("rm -rf /mnt/sdcard/mux.bin");
					}
					
					//system("killall -2 mpl_all");
					}
					INSERT_FLAG =1;
					if( access("/mnt/sdcard/LIFT", 0) == 0 )
					{	
						goto UPDATE;		
					}
					else
					{
						continue;	
					}	
										
				}
				else
				{	
					if(INSERT_FLAG == 1)
						system("reboot");
					INSERT_FLAG = 0;
				}	
			}
			
		}
	}
	
	DATECFG:
		Dt_last_weekday = -1;
		Direction_old = '0';
		weather_flag = 1;
		v4a_drawrect(&overlay_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
		v4a_drawrect(&bkgnd_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
		v4a_displayvideo(0);
		v4a_displayflex(0);
		Config_Init();
		while(1)
		{
			Display_Print_Init();
			Display_Date_Time("<Configuration Menu>");
			Display_Print(print_mac);
			Display_Print("Software Version: V4A01_1.2.3");	
			Date_Time_Config();
			
			//Display_Date_Time("VOL=[20]");
			//Display_Date_Time("LANGUAGE=CN");
			//Display_Date_Time("BKGND_TIME=[5]");
			//Display_Date_Time("LIGHT_AUTO_OFF=[15]");
			
			Volume_Config();
			Language_Config();
			Bkgnd_Time_Config();
			Light_Off_Time_Config();
			Sleep_Brightness_Config();
			
			usleep(300000);
			
			if(date_config_flag==0)
			{
				v4a_displaybkgnd(0);
				Display_Fixedbkgnd();
				goto START;
			}		
		}
	

	UPDATE:
	v4a_drawrect(&overlay_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
	v4a_drawrect(&bkgnd_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
	v4a_displayvideo(0);
	v4a_displayflex(0);
	Display_Print_Init();
	Display_Print("Find LIFT in USB Disk");
	ProcessSDStatus();
	
	while( cc=usbdisk_is_insert() );
	
	sleep(1);
	system("reboot");

	pthread_join(tid_get_lift,NULL);
	pthread_join(tid_get_lift1,NULL);
	pthread_join(tid_get_lift2,NULL);

	return 0;
}	

